using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;

using FarseerPhysics.Dynamics.Contacts;

namespace Spacer {
	public class Item : Entity {
		public Item() {}

		public Item( SolarSys world ) : base(world) {
			if( this.GetType() == typeof(Item) ) Initialize();
		}

		public override void Initialize() {
			base.Initialize();

			DrawMode = DrawMode.None;

			Invulnerable = true;

			NextActivation = 0f;
			PrimaryActivationRate = 0f;
			SecondaryActivationRate = 0f;
		}

		public override bool CreateSaveState( EntitySaveState save ) {
			base.CreateSaveState( save );

			save["Name"] = Name;
			save["ResourceWorth"] = ResourceWorth;

			return true;
		}
	
		// ----------

		public void GiveTo( Pawn ply ) {
			if( !AllowPickup(ply) ) {
				Remove();
				return;
			}

			Owner = ply;
			ply.Inventory.Add( this );

			OnPickup();
		}

        public void GiveTo(Planet plnt)
        {

            Owner = plnt;
            plnt.Inventory.Add(this);

            OnPickup();
        }

		public void ActivatePrimary() {
			if( !AllowPrimaryActivation() )
				return;

			OnActivatePrimary();

			if( PrimaryActivationSound != null )
				PrimaryActivationSound.Play();

			NextActivation = Globals.GameTime + PrimaryActivationRate;
		}

		public void ActivateSecondary() {
			if( !AllowSecondaryActivation() )
				return;

			OnActivateSecondary();

			if( SecondaryActivationSound != null )
				SecondaryActivationSound.Play();

			NextActivation = Globals.GameTime + SecondaryActivationRate;
		}

		// ----------

		public virtual bool AllowPickup( Pawn ply ) {
			return true;
		}

		public virtual bool AllowPrimaryActivation() {
			return Globals.GameTime >= NextActivation;
		}

		public virtual bool AllowSecondaryActivation() {
			return Globals.GameTime >= NextActivation;
		}

		// ----------

		public virtual void OnPickup() {
		}
		
		public virtual void OnActivatePrimary() {
		}

		public virtual void OnActivateSecondary() {
		}

		public virtual float OnOwnerTakeDamage( float amount, Entity instigator, Entity inflictor, Vector2 pos, Vector2 dir ) {
			return amount;
		}

		public virtual void OnOwnerDeath( Entity instigator, Entity inflictor ) {
			Owner = null;
			Parent = null;
			Remove();
		}

		public virtual void DrawItemCard( Vector2 pos, Vector2 size ) {
		}

		// ----------
		
		public string Name { get; protected set; }

		public float NextActivation { get; set; }
		public float PrimaryActivationRate { get; set; }
		public float SecondaryActivationRate { get; set; }
		public SoundScript PrimaryActivationSound { get; set; }
		public SoundScript SecondaryActivationSound { get; set; }

        public int ResourceWorth { get; set; }
	}
}